<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>networktester_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_net_networktester_test.js.html">networktester_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.net.NetworkTesterTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.net.NetworkTesterTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.Uri&#39;);
<a name="line19"></a>goog.require(&#39;goog.net.NetworkTester&#39;);
<a name="line20"></a>goog.require(&#39;goog.testing.MockClock&#39;);
<a name="line21"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line22"></a>
<a name="line23"></a>var clock;
<a name="line24"></a>
<a name="line25"></a>function setUp() {
<a name="line26"></a>  clock = new goog.testing.MockClock(true);
<a name="line27"></a>}
<a name="line28"></a>
<a name="line29"></a>function tearDown() {
<a name="line30"></a>  clock.dispose();
<a name="line31"></a>}
<a name="line32"></a>
<a name="line33"></a>function testSuccess() {
<a name="line34"></a>  // set up the tster
<a name="line35"></a>  var handler = new Handler();
<a name="line36"></a>  var tester = new goog.net.NetworkTester(handler.callback, handler);
<a name="line37"></a>  assertFalse(tester.isRunning());
<a name="line38"></a>  tester.start();
<a name="line39"></a>  assertTrue(handler.isEmpty());
<a name="line40"></a>  assertTrue(tester.isRunning());
<a name="line41"></a>
<a name="line42"></a>  // simulate the image load and verify
<a name="line43"></a>  var image = tester.image_;
<a name="line44"></a>  assertEquals(String(tester.getUri()), image.src);
<a name="line45"></a>  assertTrue(handler.isEmpty());
<a name="line46"></a>  image.onload.call(null);
<a name="line47"></a>  assertTrue(handler.dequeue());
<a name="line48"></a>  assertFalse(tester.isRunning());
<a name="line49"></a>}
<a name="line50"></a>
<a name="line51"></a>function testFailure() {
<a name="line52"></a>  // set up the tester
<a name="line53"></a>  var handler = new Handler();
<a name="line54"></a>  var tester = new goog.net.NetworkTester(handler.callback, handler);
<a name="line55"></a>  assertFalse(tester.isRunning());
<a name="line56"></a>  tester.start();
<a name="line57"></a>  assertTrue(handler.isEmpty());
<a name="line58"></a>  assertTrue(tester.isRunning());
<a name="line59"></a>
<a name="line60"></a>  // simulate the image failure and verify
<a name="line61"></a>  var image = tester.image_;
<a name="line62"></a>  assertEquals(String(tester.getUri()), image.src);
<a name="line63"></a>  assertTrue(handler.isEmpty());
<a name="line64"></a>  image.onerror.call(null);
<a name="line65"></a>  assertFalse(handler.dequeue());
<a name="line66"></a>  assertFalse(tester.isRunning());
<a name="line67"></a>}
<a name="line68"></a>
<a name="line69"></a>function testAbort() {
<a name="line70"></a>  // set up the tester
<a name="line71"></a>  var handler = new Handler();
<a name="line72"></a>  var tester = new goog.net.NetworkTester(handler.callback, handler);
<a name="line73"></a>  assertFalse(tester.isRunning());
<a name="line74"></a>  tester.start();
<a name="line75"></a>  assertTrue(handler.isEmpty());
<a name="line76"></a>  assertTrue(tester.isRunning());
<a name="line77"></a>
<a name="line78"></a>  // simulate the image abort and verify
<a name="line79"></a>  var image = tester.image_;
<a name="line80"></a>  assertEquals(String(tester.getUri()), image.src);
<a name="line81"></a>  assertTrue(handler.isEmpty());
<a name="line82"></a>  image.onabort.call(null);
<a name="line83"></a>  assertFalse(handler.dequeue());
<a name="line84"></a>  assertFalse(tester.isRunning());
<a name="line85"></a>}
<a name="line86"></a>
<a name="line87"></a>function testTimeout() {
<a name="line88"></a>  // set up the tester
<a name="line89"></a>  var handler = new Handler();
<a name="line90"></a>  var tester = new goog.net.NetworkTester(handler.callback, handler);
<a name="line91"></a>  assertFalse(tester.isRunning());
<a name="line92"></a>  tester.start();
<a name="line93"></a>  assertTrue(handler.isEmpty());
<a name="line94"></a>  assertTrue(tester.isRunning());
<a name="line95"></a>
<a name="line96"></a>  // simulate the image timeout and verify
<a name="line97"></a>  var image = tester.image_;
<a name="line98"></a>  assertEquals(String(tester.getUri()), image.src);
<a name="line99"></a>  assertTrue(handler.isEmpty());
<a name="line100"></a>  clock.tick(10000);
<a name="line101"></a>  assertFalse(handler.dequeue());
<a name="line102"></a>  assertFalse(tester.isRunning());
<a name="line103"></a>}
<a name="line104"></a>
<a name="line105"></a>function testRetries() {
<a name="line106"></a>  // set up the tester
<a name="line107"></a>  var handler = new Handler();
<a name="line108"></a>  var tester = new goog.net.NetworkTester(handler.callback, handler);
<a name="line109"></a>  tester.setNumRetries(1);
<a name="line110"></a>  assertFalse(tester.isRunning());
<a name="line111"></a>  tester.start();
<a name="line112"></a>  assertTrue(handler.isEmpty());
<a name="line113"></a>  assertTrue(tester.isRunning());
<a name="line114"></a>
<a name="line115"></a>  // try number 1 fails
<a name="line116"></a>  var image = tester.image_;
<a name="line117"></a>  assertEquals(String(tester.getUri()), image.src);
<a name="line118"></a>  assertTrue(handler.isEmpty());
<a name="line119"></a>  image.onerror.call(null);
<a name="line120"></a>  assertTrue(handler.isEmpty());
<a name="line121"></a>  assertTrue(tester.isRunning());
<a name="line122"></a>
<a name="line123"></a>  // try number 2 succeeds
<a name="line124"></a>  image = tester.image_;
<a name="line125"></a>  assertEquals(String(tester.getUri()), image.src);
<a name="line126"></a>  assertTrue(handler.isEmpty());
<a name="line127"></a>  image.onload.call(null);
<a name="line128"></a>  assertTrue(handler.dequeue());
<a name="line129"></a>  assertFalse(tester.isRunning());
<a name="line130"></a>}
<a name="line131"></a>
<a name="line132"></a>function testPauseBetweenRetries() {
<a name="line133"></a>  // set up the tester
<a name="line134"></a>  var handler = new Handler();
<a name="line135"></a>  var tester = new goog.net.NetworkTester(handler.callback, handler);
<a name="line136"></a>  tester.setNumRetries(1);
<a name="line137"></a>  tester.setPauseBetweenRetries(1000);
<a name="line138"></a>  assertFalse(tester.isRunning());
<a name="line139"></a>  tester.start();
<a name="line140"></a>  assertTrue(handler.isEmpty());
<a name="line141"></a>  assertTrue(tester.isRunning());
<a name="line142"></a>
<a name="line143"></a>  // try number 1 fails
<a name="line144"></a>  var image = tester.image_;
<a name="line145"></a>  assertEquals(String(tester.getUri()), image.src);
<a name="line146"></a>  assertTrue(handler.isEmpty());
<a name="line147"></a>  image.onerror.call(null);
<a name="line148"></a>  assertTrue(handler.isEmpty());
<a name="line149"></a>  assertTrue(tester.isRunning());
<a name="line150"></a>
<a name="line151"></a>  // need to pause 1000 ms for the second attempt
<a name="line152"></a>  assertNull(tester.image_);
<a name="line153"></a>  clock.tick(1000);
<a name="line154"></a>
<a name="line155"></a>  // try number 2 succeeds
<a name="line156"></a>  image = tester.image_;
<a name="line157"></a>  assertEquals(String(tester.getUri()), image.src);
<a name="line158"></a>  assertTrue(handler.isEmpty());
<a name="line159"></a>  image.onload.call(null);
<a name="line160"></a>  assertTrue(handler.dequeue());
<a name="line161"></a>  assertFalse(tester.isRunning());
<a name="line162"></a>}
<a name="line163"></a>
<a name="line164"></a>function testNonDefaultUri() {
<a name="line165"></a>  var handler = new Handler();
<a name="line166"></a>  var newUri = new goog.Uri(&#39;//www.google.com/images/cleardot2.gif&#39;);
<a name="line167"></a>  var tester = new goog.net.NetworkTester(handler.callback, handler, newUri);
<a name="line168"></a>  var testerUri = tester.getUri();
<a name="line169"></a>  assertTrue(testerUri.toString().indexOf(&#39;cleardot2&#39;) &gt; -1);
<a name="line170"></a>}
<a name="line171"></a>
<a name="line172"></a>function testOffline() {
<a name="line173"></a>
<a name="line174"></a>  // set up the tester
<a name="line175"></a>  var handler = new Handler();
<a name="line176"></a>  var tester = new goog.net.NetworkTester(handler.callback, handler);
<a name="line177"></a>  var orgGetNavigatorOffline = goog.net.NetworkTester.getNavigatorOffline_;
<a name="line178"></a>  goog.net.NetworkTester.getNavigatorOffline_ = function() {
<a name="line179"></a>    return true;
<a name="line180"></a>  };
<a name="line181"></a>  try {
<a name="line182"></a>    assertFalse(tester.isRunning());
<a name="line183"></a>    tester.start();
<a name="line184"></a>    assertTrue(handler.isEmpty());
<a name="line185"></a>    assertTrue(tester.isRunning());
<a name="line186"></a>
<a name="line187"></a>    // the call is done async
<a name="line188"></a>    clock.tick(1);
<a name="line189"></a>
<a name="line190"></a>    assertFalse(handler.dequeue());
<a name="line191"></a>    assertFalse(tester.isRunning());
<a name="line192"></a>  } finally {
<a name="line193"></a>    // Clean up!
<a name="line194"></a>    goog.net.NetworkTester.getNavigatorOffline_ = orgGetNavigatorOffline;
<a name="line195"></a>  }
<a name="line196"></a>}
<a name="line197"></a>
<a name="line198"></a>// Handler object for verifying callback
<a name="line199"></a>function Handler() {
<a name="line200"></a>  this.events_ = [];
<a name="line201"></a>}
<a name="line202"></a>
<a name="line203"></a>Handler.prototype.callback = function(result) {
<a name="line204"></a>  this.events_.push(result);
<a name="line205"></a>};
<a name="line206"></a>
<a name="line207"></a>Handler.prototype.isEmpty = function() {
<a name="line208"></a>  return this.events_.length == 0;
<a name="line209"></a>};
<a name="line210"></a>
<a name="line211"></a>Handler.prototype.dequeue = function() {
<a name="line212"></a>  if (this.isEmpty()) {
<a name="line213"></a>    throw Error(&#39;Handler is empty&#39;);
<a name="line214"></a>  }
<a name="line215"></a>  return this.events_.shift();
<a name="line216"></a>};
<a name="line217"></a>
<a name="line218"></a>// override image constructor for test - can&#39;t use a real image due to
<a name="line219"></a>// async load of images - have to simulate it
<a name="line220"></a>function Image() {
<a name="line221"></a>}
</pre>


</body>
</html>
